package com.it.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.it.aiReportClaim.ReportClaimApi;
import com.it.entity.Verification.AiClaimVerification;

import com.it.mapper.AiClaimVerificationMapper;
import com.it.service.AiClaimVerificationService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import utils.R;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.Random;

/**
 * <p>
 * 核赔信息表（对理算结果的审核确认记录） 服务实现类
 * </p>
 *
 * @author yzy
 * @since 2025年09月24日
 */
@Service
public class AiClaimVerificationServiceImpl extends ServiceImpl<AiClaimVerificationMapper, AiClaimVerification> implements AiClaimVerificationService {

    @Autowired
    private AiClaimVerificationMapper aiClaimVerificationMapper;

    @Autowired
    private ReportClaimApi reportClaimApi;

    @Override
    public R list(int pageNum, int pageSize, String reportNo, String verificationId, Integer verificationStatus) {

        // 使用PageHelper设置分页参数
        PageHelper.startPage(pageNum, pageSize);

        // 创建LambdaQueryWrapper，使用Lambda表达式避免硬编码字段名
        LambdaQueryWrapper<AiClaimVerification> queryWrapper = new LambdaQueryWrapper<>();

        // 报案号模糊查询（忽略空值）
        if (StringUtils.isNotBlank(reportNo)) {
            queryWrapper.like(AiClaimVerification::getReportNo, reportNo);
        }

        // 核赔号模糊查询（忽略空值）
        if (StringUtils.isNotBlank(verificationId)) {
            queryWrapper.like(AiClaimVerification::getVerificationId, verificationId);
        }

        // 核赔状态精确匹配（忽略空值）
        if (verificationStatus != null) {
            queryWrapper.eq(AiClaimVerification::getVerificationStatus, verificationStatus);
        }

        // 执行查询，PageHelper会自动拦截并处理分页
        List<AiClaimVerification> aiClaimVerifications = aiClaimVerificationMapper.selectList(queryWrapper);

        return R.Success("查询成功", new PageInfo<>(aiClaimVerifications));
    }

    @Override
    public R info(String verificationId) {
        if (StringUtils.isNotBlank(verificationId)) {
            AiClaimVerification aiClaimVerification = aiClaimVerificationMapper.selectById(verificationId);
            return R.Success("查询成功", aiClaimVerification);
        }
        return R.Failed("该核赔信息不存在");
    }

    @Override
    public R delete(String verificationId) {
        if (StringUtils.isNotBlank(verificationId)) {
            aiClaimVerificationMapper.deleteById(verificationId);
            return R.Success("删除成功");
        }
        return R.Failed("要删除的核赔信息不存在");
    }

    @Override
    public R add(AiClaimVerification aiClaimVerification) {

        aiClaimVerification.setVerificationId(generateVerificationId());

        aiClaimVerification.setVerificationTime(LocalDateTime.now());

        int a = aiClaimVerificationMapper.insert(aiClaimVerification);

        if (a != 0) {

            reportClaimApi.updateStatus(aiClaimVerification.getReportNo(), 7);

            return R.Success("新增核赔信息成功", aiClaimVerification);
        }
        return R.Failed("新增失败");
    }

    @Override
    public R selectByreportNo(String reportNo) {
        LambdaQueryWrapper<AiClaimVerification> queryWrapper = new LambdaQueryWrapper<>();
        if(reportNo!=null&& !reportNo.isEmpty()) {
            queryWrapper.eq(AiClaimVerification::getReportNo, reportNo);
            AiClaimVerification aiClaimVerification = aiClaimVerificationMapper.selectOne(queryWrapper);
            return R.Success("查询成功",aiClaimVerification);
        }else
            return R.Failed("查询失败，未提交正确报案号");
    }


    /**
     * 生成立案编号
     * 格式：VERIFY+yyyyMMdd+6位随机数，例如：VERIFY20250918123456
     */
    private String generateVerificationId() {
        // 前缀
        String prefix = "VERIFY";
        // 日期部分：年月日
        String datePart = LocalDate.now().format(DateTimeFormatter.ofPattern("yyyyMMdd"));
        // 随机数部分：6位随机数
        String randomPart = String.format("%06d", new Random().nextInt(1000000));

        // 组合生成核赔编号
        return prefix + datePart + randomPart;
    }

}
